package network;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket = null;

    public UdpEchoClient() throws SocketException {
        // 客户端的端口号一般自动分配
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            // 1. 让客户端从控制台读取一个请求数据
            System.out.print("> ");
            String request = scanner.next();
            // 2. 把这个字符串发送给服务器，构造 DatagramPacket
            //    构造的 Packet 既要包含 要传输的数据，又要包含把数据发送到哪
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(), request.getBytes().length,
                    InetAddress.getByName("127.0.0.1"), 8000);
            // 3. 把数据发送给服务器
            socket.send(requestPacket);
            // 4. 从服务器读取响应
            DatagramPacket responPacket = new DatagramPacket(new byte[4096], 4096);
            socket.receive(responPacket);
            // 5. 把响应数据获取出来，转成字符串
            String response = new String(responPacket.getData(), 0, responPacket.getLength());

            System.out.printf("req: %s; resp: %s\n", request, response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client = new UdpEchoClient();
        client.start();
    }
}
